package org.xtb.CS_2;

import org.xtb.pojo.Contacts;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private static List<Contacts> contacts = new ArrayList<>();
    static {
        contacts.add(new Contacts("张三","四川成都新都区","18956254578"));
        contacts.add(new Contacts("李四","上海东圃新区","15956477423"));
        contacts.add(new Contacts("王五","广东广州市","18035896541"));
    }

    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(8888);
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                Socket clientSocket = serverSocket.accept();
                System.out.println("客户端已连接");

                ClientHandler clientHandler = new ClientHandler(clientSocket);
                clientHandler.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class ClientHandler extends Thread {
        private Socket clientSocket;

        public ClientHandler(Socket clientSocket) {
            this.clientSocket = clientSocket;
        }

        public void run() {
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    System.out.println("客户端请求：" + inputLine);

                    switch (inputLine) {
                        case "添加联系人":
                            String name = in.readLine();
                            String address = in.readLine();
                            String phone = in.readLine();

                            contacts.add(new Contacts(name, address, phone));
                            out.println("联系人添加成功！");
                            break;
                        case "修改联系人":
                            // 实现修改联系人的逻辑
                            out.println("请输入要修改的联系人姓名：");
                            String name1 = in.readLine();

                            Contacts contact = findContactByName(name1,contacts);
                            if (contact != null) {
                                out.println("请输入新的联系人姓名：");
                                String newName = in.readLine();
                                out.println("请输入新的联系人住址：");
                                String newAddress = in.readLine();
                                out.println("请输入新的联系人电话：");
                                String newPhone = in.readLine();

                                contact.setName(newName);
                                contact.setAddress(newAddress);
                                contact.setPhone(newPhone);

                                out.println("联系人修改成功！");
                            } else {
                                out.println("联系人不存在！");
                            }
                            break;
                        case "删除联系人":
                            // 实现删除联系人的逻辑
                            String name2 = in.readLine();
                            Contacts contactByName = findContactByName(name2, contacts);
                            out.println("删除联系人成功！");
                            contacts.remove(contactByName);
                            out.println(getContactList(contacts));
                            break;
                        case "查看联系人":
                            String response = getContactList(contacts);
                            out.println(response);

                            break;
                        default:
                            out.println("无效的请求");
                            break;
                    }
                }

                in.close();
                out.close();
                clientSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private Contacts findContactByName(String name,List<Contacts> contacts) {
            for (Contacts contact : contacts) {
                if (contact.getName().equals(name)) {
                    return contact;
                }
            }
            return null;
        }
        private String getContactList(List<Contacts> contacts) {
            if (contacts.isEmpty()) {
                return "通讯录为空";
            } else {
                StringBuilder sb = new StringBuilder();
                sb.append("联系人列表：\n");
                sb.append("===============================================\n");
                sb.append("姓名\t\t住址\t\t电话\n");
                sb.append("===============================================\n");
                for (Contacts contact : contacts) {
                    sb.append(contact.getName()).append("\t\t").append(contact.getAddress()).append("\t\t").append(contact.getPhone()).append("\n");
                }
                sb.append("===============================================\n");
                sb.append("列表结尾");
                return sb.toString();
            }
        }
    }

}